<template>
	<view class="container-list">
		<view class="container-scroll">
			<z-paging ref="paging" v-model="collegeList" @query="getCollegeList" :fixed="false" :auto="false"
				:empty-view-style="{'margin-top':'-200rpx'}">

				<search-collegecell class="college-item" v-for="(item,index) in collegeList" :key="item.sid"
					:isShowContrastBtn="false" :data="item" :isSelect="getFindIndexToContrastList(item) >= 0"
					@tap="onClickCollegeCell(item)" @onClickAddContrast="onClickAddContrast"></search-collegecell>

			</z-paging>
		</view>
		<loading v-show="loadshow"></loading>
	</view>
</template>

<script>
	export default {

		props: {
			inputValue: {
				type: String,
				default: ''
			},
			isShowData: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				loadshow: false,
				collegeList: [],
				page: 1,
				searchKey: "", //搜索的值
				selFilterDatas: {},
				contrastList: [],
			}
		},
		methods: {

			// 获取学校列表数据
			getCollegeList(page, pageSize) {
				// if(this.isShowData==2){
				// 	uni.showLoading({
				// 		mask:true
				// 	})
				// }

				let data = {}
				data["page"] = page //this.page
				data["pageSize"] = pageSize

				if (this.searchKey.length > 0) {
					data["search"] = this.searchKey
				}
				if (this.selFilterDatas["province"]) {
					let province = this.selFilterDatas["province"].toString()
					data["province"] = province
				}
				if (this.selFilterDatas["type"]) {
					let type = this.selFilterDatas["type"].toString()
					data["type"] = type
				}
				if (this.selFilterDatas["nature"]) {
					let nature = this.selFilterDatas["nature"].toString()
					data["nature"] = nature
				}
				if (this.selFilterDatas["school_type"]) {
					let school_type = this.selFilterDatas["school_type"].toString()
					data["school_type"] = school_type
				}
				if (this.selFilterDatas["school_feature"]) {
					let school_feature = this.selFilterDatas["school_feature"].toString()
					data["school_feature"] = school_feature
				}

				this.loadshow = true
				this.$request.searchSchool(data).then(res => {
					this.loadshow = false
					if (this.page == 1) {
						this.collegeList = res.data
						if (!this.artcleMsgData) {
							this.$emit("isLookMet", "schoolMassage")
						}
					} else {
						this.collegeList = this.collegeList.concat(res.data)
					}
					this.$refs.paging.complete(res.data);
					// uni.hideLoading()
				}).catch(err => {
					// uni.hideLoading()
					uni.$emit('z-paging-error-emit')
				})
			},

			onClickAddContrast(data) {
				let index = this.getFindIndexToContrastList(data)
				if (index >= 0) {
					this.contrastList.splice(index, 1)
				} else {
					this.contrastList.unshift(data)
				}

				this.$userManager.setContrastList(this.contrastList)
			},



			// 获取元素在ContrastList数组中的下标
			getFindIndexToContrastList(data) {
				return this.contrastList.findIndex(item => item.sid == data.sid)
			},
			// 点击学校列表
			onClickCollegeCell(college) {

				// if (this.$userManager.isLogin()) {
				uni.navigateTo({
					url: '/subpackages/subpackage1/pages/searchcollege/collegeDetails/college-details?data=' + JSON.stringify(college)
				})
				// }
			},

		},

		watch: {
			inputValue: {
				handler() {
					this.searchKey = this.inputValue
					this.getCollegeList(1, 10)
				},
				deep: true,
				immediate: true
			}

		},
		onShow() {
			const list = this.$userManager.getContrastList()
			this.contrastList = list ? list : []
		},
		mounted() {
			this.searchKey = this.inputValue
			// this.getCollegeList(1, 10)
			// this.$refs.paging.reload();
		},

	}
</script>

<style lang="scss" scoped>
	.container-list {
		position: relative;
		height: 100%;
		width: 100vw;
	}

	.container-scroll {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		width: 100vw;
		height: 100%;
	}
</style>